package com.qfedu.vhr.framework.service.impl;

import com.qfedu.vhr.common.RespBean;
import com.qfedu.vhr.framework.entity.HrDetails;
import com.qfedu.vhr.framework.entity.Menu;
import com.qfedu.vhr.framework.entity.vo.MenuVO;
import com.qfedu.vhr.framework.entity.vo.MenuWithRole;
import com.qfedu.vhr.framework.mapper.MenuMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qfedu.vhr.framework.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangsan
 * @since 2023-01-09
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Autowired
    MenuMapper menuMapper;

    @Override
    public List<MenuVO> getAllMenusByHrId() {
        return menuMapper.getAllMenusByHrId(((HrDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId());
    }

    @Override
    public List<MenuWithRole> getMenusWithRole() {
        return menuMapper.getMenusWithRole();
    }

    @Override
    public List<MenuVO> getAllMenus() {
        return menuMapper.getAllMenus();
    }

    @Override
    public List<Integer> getSelectedMenuIdByRoleId(Integer rid) {
        return menuMapper.getSelectedMenuIdByRoleId(rid);
    }

    @Transactional
    @Override
    public RespBean updateMenuRole(Integer rid, Integer[] mids) {
        try {
            menuMapper.deleteMenuRoleByRid(rid);
            menuMapper.addMenuRole(rid, mids);
            return RespBean.ok("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RespBean.error("更新失败");
    }
}